﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using System.Drawing;
using System.Data.Common;

namespace MacomberMapSystem.Common.Database
{
    /// <summary>
    /// This class holds information on a database user
    /// </summary>
    public class MM_Database_User: MM_Serializable, IComparable<MM_Database_User>
    {
        #region Variable declarations
        /// <summary>The full name name of the user</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String FullName;

        /// <summary>The email address of the user</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Email;

        /// <summary>The user name of the individual</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String UserName;

        /// <summary>The image of the user</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public Bitmap UserImage;

        /// <summary>The state of the user</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public enumUserState UserState;

        /// <summary>The user's nickname</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Nickname;

        /// <summary>Whether the user can handle error assignments</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public bool HandleErrors;

        /// <summary>Whether the user can handle warnings-only assignments</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public bool HandleWarnings;

        /// <summary>Whether the user can handle blackstart corridor update assignments</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public bool HandleBlackstartCorridors; 

        /// <summary>The user's note</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String UserNote;

        /// <summary>When this record was last updated</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public DateTime LastUpdate;

        /// <summary>Whether this user is visible in the viewer when its' count is 0</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public bool Visible=true;


        /// <summary>
        /// The collection of states for the user
        /// </summary>
        public enum enumUserState
        {
            /// <summary>The user is available for assignments</summary>
            Available,

            /// <summary>The user is not available for assignments (generic reason)</summary>
            NotAvailable,

            /// <summary>The user is out of the office</summary>
            OutOfOffice
        }

        /// <summary>The event handler for a value change</summary>
        public EventHandler ValueChanged;

        /// <summary>The list of one-lines assigned to the user</summary>
        public int AssignedOneLines;

        /// <summary>The number of fixes assigned to the user</summary>
        public int AssignedFixes;

        /// <summary>The list of one-lines completed by the user</summary>
        public int CompletedOneLines;

        /// <summary>The list of one-line fixes by the user</summary>
        public int CompletedFixes;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new database user
        /// </summary>
        /// <param name="sRd"></param>
        /// <param name="Repository">The data repository</param>
        public MM_Database_User(DbDataReader sRd,MM_Repository Repository)
            : base(sRd, null,Repository)
        {
        }

        /// <summary>
        /// Initialize a new user
        /// </summary>
        /// <param name="FullName"></param>
        /// <param name="UserName"></param>
        /// <param name="Email"></param>
        /// <param name="Nickname"></param>
        /// <param name="UserState"></param>
        /// <param name="UserImage"></param>
        /// <param name="Repository">The data repository</param>
        public MM_Database_User(String FullName, String UserName, String Email, String Nickname, enumUserState  UserState, Bitmap UserImage, MM_Repository Repository)
            : base(null,Repository)
        {
            this.FullName = FullName;
            this.UserImage = UserImage;
            this.UserName = UserName;
            this.Email = Email;
            this.Nickname = Nickname;
            this.UserState = UserState;
            this.LastUpdate = DateTime.Now;
        }
        #endregion

        /// <summary>
        /// Compare this user to another
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(MM_Database_User other)
        {
            if (this.Nickname == other.Nickname)
                return 0;
            else if (this.Nickname == "Total")
                return 1;
            else if (other.Nickname == "Total")
                return -1;
            else
                return this.Nickname.CompareTo(other.Nickname);
        }

        /// <summary>
        /// Report the user in an easy-to-read string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Nickname;
        }
    }
}
